package eu.boss.activities;

import java.io.IOException;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import eu.boss.R;
import eu.boss.model.Player;

/**
 * This class describes the player activity. Displays stats.
 * 
 * @author Arnaud
 */

public class PlayerActivity extends Activity {
	/**
	 * @param mMapper Mapper to map JSON data
	 * @param mPlayer 
	 */
	private ObjectMapper mMapper = null;
	private Player mPlayer = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.player);

		// Getting player from previous activity
		mMapper = new ObjectMapper();
		Bundle objetbunble = this.getIntent().getExtras();

		if (objetbunble != null && objetbunble.containsKey("player")) {
			String player = this.getIntent().getStringExtra("player");
			displayResults(player);
		}
	}

	/**
	 * Display results in different views.
	 * 
	 * @param strPlayer
	 *            player in json format
	 */
	public void displayResults(String strPlayer) {

		try {
			//mapping strPlayer to create a Player type. It's pretty impossible to have an error so i don't create a specific alertview
			mPlayer = mMapper.readValue(strPlayer, Player.class);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//Initiate fields
		TextView tvSw = (TextView) findViewById(R.id.tvSW);
		TextView tvSl = (TextView) findViewById(R.id.tvSL);
		TextView tvHw = (TextView) findViewById(R.id.tvHW);
		TextView tvHl = (TextView) findViewById(R.id.tvHL);
		TextView tvNw = (TextView) findViewById(R.id.tvNW);
		TextView tvNl = (TextView) findViewById(R.id.tvNL);
		TextView tvTotalW = (TextView) findViewById(R.id.tvTW);
		TextView tvTotalL = (TextView) findViewById(R.id.tvTL);
		TextView tvRatio = (TextView) findViewById(R.id.tvTR);
		TextView tvPlayerName = (TextView) findViewById(R.id.tvPlayerName);
		Button btnDelete = (Button) findViewById(R.id.btnDelete);
		
		//Displaying all results in fields
		tvSl.setText(String.valueOf(mPlayer.getLostShadow()));
		tvSw.setText(String.valueOf(mPlayer.getWonShadow()));
		tvHw.setText(String.valueOf(mPlayer.getWonHunter()));
		tvHl.setText(String.valueOf(mPlayer.getLostHunter()));
		tvNw.setText(String.valueOf(mPlayer.getWonNeutral()));
		tvNl.setText(String.valueOf(mPlayer.getLostNeutral()));
		tvTotalW.setText(String.valueOf(mPlayer.getWins()));
		tvTotalL.setText(String.valueOf(mPlayer.getLosts()));
		tvRatio.setText(String.valueOf(mPlayer.getRatio()));
		tvPlayerName.setText(mPlayer.getName());

		//onCLickListener on "Delete" button
		btnDelete.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {

				AlertDialog.Builder adb = new AlertDialog.Builder(
						PlayerActivity.this);
				adb.setTitle(getString(R.string.confirm));
				adb.setMessage(getString(R.string.are_you_sure));
				adb.setPositiveButton("Ok",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								Intent intent = new Intent();
								setResult(RESULT_OK, intent);
								finish();
							}
						});
				adb.setNegativeButton("Annuler", null);
				adb.show();
			}
		});
	}
}
